package com.clear.auth.application.controller;

import com.alibaba.fastjson.JSON;
import com.clear.auth.application.convert.AuthPermissionDTOConverter;
import com.clear.auth.application.dto.AuthPermissionDTO;
import com.clear.auth.domain.entity.AuthPermissionBO;
import com.clear.auth.domain.service.AuthPermissionDomainService;
import com.clear.auth.entity.Result;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 权限Controller
 */
@RestController
@RequestMapping("/permission")
@Slf4j
public class PermissionController {

    @Resource
    private AuthPermissionDomainService authPermissionDomainService;

    /**
     * 新增权限
     *
     * @param authPermissionDTO
     * @return
     */
    @PostMapping("add")
    public Result<Boolean> add(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("PermissionController.add.dto:{}", JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(authPermissionDTO.getName()), "权限名称不能为空");
            Preconditions.checkNotNull(authPermissionDTO.getParentId(), "权限父id不能为空");
            Preconditions.checkArgument(!StringUtils.isBlank(authPermissionDTO.getPermissionKey()), "权限唯一标识不能为空");

            AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.INSTANCE.converterDTOToBO(authPermissionDTO);

            Boolean result = authPermissionDomainService.add(authPermissionBO);

            return Result.ok(result);
        } catch (Exception e) {
            log.error("PermissionController.add.error:{}", e.getMessage(), e);
            return Result.fail("新增权限失败");
        }
    }

    /**
     * 更新权限
     *
     * @param authPermissionDTO
     * @return
     */
    @PostMapping("update")
    public Result<Boolean> update(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("PermissionController.update.dto:{}", JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkNotNull(authPermissionDTO.getId(), "权限id不能为空");

            AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.INSTANCE.converterDTOToBO(authPermissionDTO);

            Boolean result = authPermissionDomainService.update(authPermissionBO);

            return Result.ok(result);
        } catch (Exception e) {
            log.error("PermissionController.update.error:{}", e.getMessage(), e);
            return Result.ok("更新权限信息失败");
        }
    }

    /**
     * 删除权限
     *
     * @param authPermissionDTO
     * @return
     */
    @PostMapping("delete")
    public Result<Boolean> delete(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("PermissionController.delete.dto:{}", JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkNotNull(authPermissionDTO.getId(), "权限id不能为空");

            AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.INSTANCE.converterDTOToBO(authPermissionDTO);

            Boolean result = authPermissionDomainService.delete(authPermissionBO);

            return Result.ok(result);
        } catch (Exception e) {
            log.error("PermissionController.delete.error:{}", e.getMessage(), e);
            return Result.ok("删除权限信息失败");
        }
    }

    /**
     * 查询用户权限
     *
     * @param username 用户名 openId
     * @return
     */
    @GetMapping("getPermission")
    public Result<List> getPermission(@RequestParam("userName") String username) {
        try {
            log.info("PermissionController.getPermission.username:{}", username);

            Preconditions.checkArgument(!StringUtils.isBlank(username), "用户名不能为空");

            List<String> permissionList = authPermissionDomainService.getPermission(username);

            return Result.ok(permissionList);
        } catch (Exception e) {
            log.error("PermissionController.getPermission.error:{}", e.getMessage(), e);
            return Result.ok("查询用户权限信息失败");
        }
    }

}
